<template>
  <div class="comment-reply">
    <van-nav-bar :title="comment.reply_count>0 ? `${comment.reply_count}条回复`:'暂无回复'">
      <van-icon @click="$emit('close')" slot="left" name="cross"></van-icon>
    </van-nav-bar>
    <div class="scroll-wrap">
      <!-- 当前评论组件 -->
      <comment-item :comment="comment"></comment-item>
      <!-- /当前评论组件 -->

      <!-- 评论回复列表 -->
      <van-cell title="全部回复"></van-cell>
      <!-- 当前组件设置commentList然后和list进行共享数据。 -->
      <comment-list :list="commentList" :source="comment.com_id" type="c"></comment-list>
      <!--/评论回复列表 -->
    </div>

    <!-- 底部区域 -->
    <div class="post-wrap">
      <van-button class="post-btn" size="small" @click="isPostShow=true">写评论</van-button>
    </div>
    <!-- /底部区域 -->

    <!-- 发布评论 -->
    <van-popup v-model="isPostShow" position="bottom">
      <comment-post @post-success="onPostSuccess" :target="comment.com_id"></comment-post>
    </van-popup>
    <!-- /发布评论 -->


  </div>
</template>
<script>
import CommentItem from "./comment-item"
import CommentList from "./comment-list"
import CommentPost from "./comment-post"
export default {
    name:'CommentReply',
    data(){
        return{ 
          isPostShow:false,  // 回复输入框
          commentList:[]  // 评论列表数据，用于共享评论列表
        }
    },
    props:{
      comment:{
        type:Object,
        required:true
      }
    },
    methods:{
      onPostSuccess(data){
        // 更新回复总数量
        this.comment.reply_count++;
        // 关闭弹层
        this.isPostShow = false;
        // 将回复内容展示到列表最顶部
        this.commentList.unshift(data.new_obj)
      }
    },
    components:{
      CommentItem,
      CommentList,
      CommentPost
    }
}
</script>
<style scoped lang="less">
.scroll-wrap{
  position: fixed;
  top: 92px;
  left: 0;
  right: 0;
  bottom: 88px;
  overflow-y: scroll;
}
.post-wrap{
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 88px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  border-top: 1px solid #d8d8d8;
  .post-btn{
    width: 60%;
  }
}
</style>